package com.ccg.service;

import com.ccg.dao.RoomDao;
import com.ccg.entity.Date_Rt_Number;
import com.ccg.entity.Message;
import com.ccg.entity.Room;
import com.ccg.entity.RoomType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomService {
    @Autowired
    RoomDao roomDao;
    @Autowired
    Date_Rt_NumberService date_rt_numberService;
    @Autowired
    RoomTypeService roomTypeService;

    //    添加房间
    //    以及同步roomType以及date_rt_number的相关数据
    @Transactional
    public Message addRoom(Room room){
        int num = roomDao.addRoom(room);
        if(num == 1){
            updateRt_drn_New(room.getRt_id());
            return new Message(0,"添加房间成功",null);
        }
        else return new Message(1,"添加房间失败","异常错误");
    }

    //按房间编号查找房间
    public Room findRoomById(int room_id) {
        return roomDao.findRoomById(room_id);
    }

    //根据房间编号以及房间类型修改房间的状态
    public int updateStatusById(Room room) {
        return roomDao.updateStatusById(room);
    }

    //根据房间编号修改房间的类型
    //需同步roomtype以及date_rt_number的相关数据
    @Transactional
    public Message updateTypeById(Room room) {
        Message message = null;
        boolean isFull = false;    //用于判断该房间类型在某一天是否被使用完，如果是则不能修改房间类型
        //判断当前房间是否有人入住
        if(findRoomById(room.getRoom_id()).getStatus()==0)
            return new Message(1,"不允许修改该房间的类型","该房间当前有人入住");
        //获取原来的房间类型
        int oldTy_id = findRoomById(room.getRoom_id()).getRt_id();
        List<Date_Rt_Number> list = date_rt_numberService.getAllByRt(oldTy_id);
        for(Date_Rt_Number drn:list){
            //判断该房间类型在某一天是否使用完
            if(drn.getNumber()==0){
                isFull = true;
                message = new Message(1,"不允许修改房间类型","该房间类型在"+drn.getDate()+"号被使用完");
                break;
            }
        }
        //该房间类型都没有使用完,允许修改
        if(!isFull) {
            //RoomType roomType = new RoomType();
            roomDao.updateTypeById(room);
            updateRt_drn_Old(oldTy_id);
            updateRt_drn_New(room.getRt_id());
            message = new Message(0,"修改房间类型成功",null);
        }
//        else message = new Message(1,"修改房间类型失败","未知异常");
        return message;
    }

    //根据房间编号修改房间的床数
    public Message updateBedByRoom(Room room){
        if(roomDao.updateBedByRoom(room)==1)
        return new Message(0,"修改成功",null);
        else return new Message(1,"修改失败",null);
    }

    //根据房间状态和类型查找房间
    public List<Room> findRoomByStatus_Type(Room room) {
        return roomDao.findRoomByStatus_Type(room);
    }

    //根据房间类型查找房间
    public List<Room> findRoomByType(int type){
        return roomDao.findRoomByType(type);
    }
    //用于房间类型改变时修改相应的表roomType,date_rt_number
    //用于将相关的资源减1
    private void updateRt_drn_Old(int oldRt_id){
        Date_Rt_Number date_rt_number = new Date_Rt_Number();
        date_rt_number.setRt_id(oldRt_id);
        date_rt_number.setNumber(-1);
        date_rt_numberService.updateNumberByRt_id(date_rt_number);
        RoomType roomType = new RoomType();
        roomType.setRt_id(oldRt_id);
        roomType.setNumber(-1);
        roomTypeService.updateNumberByRt_id(roomType);
    }
    //用于房间类型改变时修改相应的表roomType,date_rt_number
    //用于将相关的资源加1
    private void updateRt_drn_New(int newRt_id){
        Date_Rt_Number date_rt_number = new Date_Rt_Number();
        date_rt_number.setRt_id(newRt_id);
        date_rt_number.setNumber(1);
        date_rt_numberService.updateNumberByRt_id(date_rt_number);
        RoomType roomType = new RoomType();
        roomType.setRt_id(newRt_id);
        roomType.setNumber(1);
        roomTypeService.updateNumberByRt_id(roomType);
    }
}